home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / MemoryHe.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  8.0 KB  |  251 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemoryHe.h
  3.  
  4.     Contains:    MemoryHeap class interface
  5.  
  6.     Owned by:    Michael Burbidge, Jens Alfke
  7.     Owned by:    Jens Alfke
  8.  
  9.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.          <3>    27.09.1996    NP        1386083: Changes to Allocate
  14.          <2>     9/13/96    jpa        1371387: Speed optimizations.
  15.         <11>    10/24/95    jpa        1293441: Added slush-fund.
  16.         <10>      8/4/95    DM        Leak checking [1267956]
  17.          <9>      5/4/95    jpa        Support for finding largest free block
  18.                                     [1235657] and validating memory ranges
  19.                                     [1246077]
  20.          <8>    10/24/94    jpa        Constness [1194286].
  21.          <7>     9/29/94    RA        1189812: Mods for 68K build.
  22.          <6>     9/14/94    jpa        Eliminated dependencies on rest of OpenDoc.
  23.                                     Added ability to get heap of a block.
  24.                                     [1186692]
  25.          <5>     8/17/94    jpa        Added support for walking heaps [1179567].
  26.          <4>      8/8/94    jpa        Added oldBlk param to DidRealloc hook
  27.                                     [1179567]
  28.          <3>     6/28/94    jpa        Added no-op new/delete to MemoryHookList to
  29.                                     avoid refs to ODDisposePtr.
  30.          <2>     6/13/94    MB        Some more initial fixes
  31.          <2>     6/10/94    MB        Make it build
  32.          <1>      6/9/94    MB        first checked in
  33.          <3>     5/26/94    MB        #1162181: Fixed MMM integration bug
  34.          <2>      5/9/94    MB        #1162181: Changes necessary to install MMM.
  35.          <1>     4/29/94    MB        first checked in
  36.     To Do:
  37.     In Progress:
  38.         
  39. */
  40.  
  41. #ifndef _MEMORYHE_
  42. #define _MEMORYHE_
  43.  
  44. #ifndef _PLATFMEM_
  45. #include "PlatfMem.h"
  46. #endif
  47.  
  48. #ifndef _MEMHOOK_
  49. #include "MemHook.h"
  50. #endif
  51.  
  52. #ifndef __STDDEF__
  53. #include <stddef.h>
  54. #endif
  55.  
  56. #ifndef __TYPES__
  57. #include <Types.h>
  58. #endif
  59.  
  60.  
  61. //----------------------------------------------------------------------------------------
  62. // HeapWalkProc (proc-ptr type)
  63. //----------------------------------------------------------------------------------------
  64.  
  65. typedef MMBoolean (*HeapWalkProc)( const void *blk, unsigned long size, MMBoolean isObject,
  66.                                  void *refCon );
  67.  
  68. class StackCrawl;        // For GetBlockStackCrawl
  69.     
  70.  
  71. //========================================================================================
  72. // MemoryHeap
  73. //
  74. //        Abstract base class for memory heaps.
  75. //
  76. //========================================================================================
  77.  
  78. class MemoryHeap
  79. {
  80. public:
  81.     enum { kBlockTypeId = 0 };
  82.     enum { kMagicNumber = 0x8765FEDC };
  83.  
  84.     // Keep a list of all heaps created. Very useful for debugging.
  85.     
  86.     static MemoryHeap* fHeapList;
  87.     static MemoryHeap* GetFirstHeap();
  88.  
  89.     void* Allocate(ODBlockSize size);
  90.     ODBlockSize BlockSize(const void* block) const;
  91.     virtual unsigned long BytesAllocated() const;
  92.     virtual unsigned long BytesFree() const = 0;
  93.     virtual unsigned long LargestFreeBlock() const;
  94.     
  95.     void Free(void*);
  96.     virtual MemoryHeap* GetNextHeap() const;
  97.     virtual unsigned long HeapSize() const = 0;
  98.     virtual unsigned long NumberAllocatedBlocks() const;
  99. //    void Reset();
  100.     void* Reallocate(void* block, ODBlockSize newSize);
  101.  
  102.     inline MMHeapLocation GetLocation( )        {return fMemSource;}
  103.  
  104.     void* operator new(SIZE_T size, MMHeapLocation =kMMTempMemory);
  105.     void operator delete(void* ptr);
  106.  
  107.     virtual ~MemoryHeap();
  108.     
  109.     static const char* kDefaultDescription;
  110.     static const char* kDeadHeapDescription;
  111.     virtual const char* GetDescription() const;
  112.     virtual void SetDescription(const char* description = kDefaultDescription);
  113.  
  114.     // Access to the isObject flag of a block. The intended use is that 'operator new'
  115.     // will set the flag for all SOM objects, giving us a way to tell whether any block
  116.     // is an object. This can help a lot in debugging.    --jpa
  117.     void SetBlockIsObject( void* blk, mmboolean isObject );
  118.     mmboolean BlockIsObject( const void* blk ) const;
  119.  
  120.     MemoryHeap* GetBlockHeap( const void* ) const;    // Should be static but can't be made so
  121.  
  122.     mmboolean    AllocateSlushFund( size_t size, size_t allocSizeLimit );
  123.     size_t        GetSlushFundSize( )                {return fSlushFundSize;}
  124.     size_t        FreeSlushFund( );                // returns size freed
  125.  
  126. #if MM_DEBUG
  127.     virtual void AdoptHook(ODMemoryHook* ODMemoryHook);
  128.     virtual void DeleteHook(ODMemoryHook* ODMemoryHook);
  129.  
  130.     virtual void SetZapOnAllocate(mmboolean = false);
  131.     virtual void SetZapOnFree(mmboolean = false);
  132.     virtual mmboolean GetZapOnAllocate() const;
  133.     virtual mmboolean GetZapOnFree() const;
  134.     virtual mmboolean GetAutoValidation() const;
  135.     virtual void SetAutoValidation(mmboolean = false);
  136.     mmboolean IsValidBlock(const void* blk) const;
  137.     virtual mmboolean IsMyBlock(const void* blk) const = 0;
  138.     mmboolean FindBlockContaining( const void *start, const void *end,
  139.                         const void* &blockStart, const void* &blockEnd ) const;
  140.     
  141.     ODBlockSize GetBlockRealSize( const void* blk ) const;
  142.  
  143.     virtual void Check( HeapWalkProc proc =NULL, void *refCon =NULL ) const = 0;
  144.     virtual void Print(char* msg = "") const = 0;
  145.  
  146.     // Access to block StackCrawl.
  147.     void SetBlockStackCrawl( const void* blk, StackCrawl* );
  148.     StackCrawl* GetBlockStackCrawl( const void* blk ) const;
  149. #endif
  150.  
  151.     MemoryHeap(mmboolean autoValidation = false,
  152.                    mmboolean zapOnAllocate = true,
  153.                    mmboolean zapOnFree = false,
  154.                    MMHeapLocation =kMMTempMemory);
  155.  
  156.     virtual void* AllocateRawMemory(ODBlockSize size);
  157.     virtual void* DoAllocate(ODBlockSize size, ODBlockSize& allocatedSize) = 0;
  158.     virtual ODBlockSize DoBlockSize(const void* block) const  = 0;
  159.     virtual void DoFree(void*) = 0;
  160. //    virtual void DoReset() = 0;
  161.     virtual void* DoReallocate(void* block, ODBlockSize newSize, ODBlockSize& allocatedSize);
  162.     virtual void FreeRawMemory(void* ptr);
  163.     virtual unsigned long DoLargestFreeBlock() const = 0;
  164.     
  165.     virtual void DoSetBlockIsObject( void* ptr, mmboolean isObject ) = 0;
  166.     virtual mmboolean DoBlockIsObject( const void* ptr ) const = 0;
  167.     virtual MemoryHeap* DoGetBlockHeap( const void* ) const = 0;
  168.  
  169. #if MM_DEBUG
  170.     virtual mmboolean    DoIsValidBlock(const void* blk) const  = 0;
  171.     virtual mmboolean    DoFindBlockContaining( const void *start, const void *end,
  172.                                                 const void* &blockStart, const void* &blockEnd ) const = 0;
  173.     virtual void        CompilerCheck();
  174.     void                SetMemFullMark();
  175.     void                UnsetMemFullMark();
  176. #endif
  177.  
  178. #if MM_DEBUG
  179.     mmboolean ValidateMagicNumber( ) const;
  180. #else
  181.     inline void ValidateMagicNumber( ) const{ }
  182. #endif
  183.  
  184. protected:
  185.     void    AddToBytesAllocated( ODBlockSize bytes )    {fBytesAllocated += bytes;}
  186.     void    SubtractFromBytesAllocated( ODBlockSize bytes )    {fBytesAllocated -= bytes;}
  187.  
  188. private:
  189.     MemoryHeap* fNextHeap;
  190.     MMHeapLocation fMemSource;
  191. #if MM_DEBUG
  192.     mmboolean    fZapOnAllocate;
  193.     mmboolean    fZapOnFree;
  194.     mmboolean    fAutoValidation;
  195.     size_t        fMemFullMark; // 0 means there is no mark.
  196. #endif
  197.     unsigned long fBytesAllocated;
  198.     unsigned long fNumberAllocatedBlocks;
  199.     long fMagicNumber;
  200.     MMBlock fSlushFund;
  201.     size_t    fSlushFundSize;
  202.     size_t    fSlushFundAllocSizeLimit;    // Max request size to use slush fund for
  203.  
  204.     enum { kDescriptionLength = 64 };
  205.     char fDescription[kDescriptionLength];
  206.     
  207. #if MM_DEBUG
  208.     MemoryHookList fMemoryHookList;
  209.  
  210. protected:
  211.     ODBlockSize CallGetHeaderSize() const;
  212.     
  213.     ODBlockSize CallAboutToAllocateHooks(ODBlockSize size) const;
  214.     void* CallDidAllocateHooks(void* blk, ODBlockSize size);
  215.     const void* CallAboutToBlockSizeHooks(const void* blk) const;
  216.     void* CallAboutToFreeHooks(void* blk);
  217.     void CallAboutToReallocHooks(void*& blk, ODBlockSize& size);
  218.     void* CallDidReallocHooks(void* oldBlk, void* blk, ODBlockSize size);
  219.     void CallAboutToResetHooks();
  220.     void CallCommentHooks(const char* comment);
  221.     void ValidateAndReport(void* blk) const;
  222. #endif
  223.  
  224. private:
  225.     MemoryHeap(const MemoryHeap& blk);
  226.     MemoryHeap& operator=(const MemoryHeap& blk);
  227.         // This class shouldn't be copied.
  228. };
  229.  
  230.  
  231. //----------------------------------------------------------------------------------------
  232. // MemoryHeap::GetBlockHeap
  233. //----------------------------------------------------------------------------------------
  234. inline MemoryHeap* MemoryHeap::GetBlockHeap( const void* ptr ) const
  235. {
  236.     // This makes the assumption (in a debug build) that the block's heap
  237.     // has the same memory hooks installed as this one. In reality this is the
  238.     // case; but at some future point it might cause trouble......
  239.     // In any case this will not occur in a non-debug build.
  240.     
  241.     this->ValidateMagicNumber();
  242. #if MM_DEBUG
  243.     ptr = this->CallAboutToBlockSizeHooks(ptr);
  244. #endif
  245.     return this->DoGetBlockHeap(ptr);
  246. }
  247.  
  248.  
  249.  
  250. #endif
  251.